import { ESpecimenStatus } from "@scmt/api-common";
import { IAction } from "@scmt/common-components";
import {
  useCurrentSpecimen,
  useSelectSpecimens,
  useSpecimenStatus,
} from "./specimen";

export const useSpecimenButtons = () => {
  const { specimenStatus } = useSpecimenStatus();
  const { selectSpecimens } = useSelectSpecimens();
  const { currentSpecimen } = useCurrentSpecimen();

  const listActions = computed(() =>
    getButtonByStatus(specimenStatus.value, true).map((i) => {
      const action = Object.assign({}, i, {
        disabled: selectSpecimens.value.length === 0,
      });

      if (action.event === "merge-print") {
        Object.assign(action, {
          disabled:
            selectSpecimens.value.length < 2 ||
            selectSpecimens.value.some(
              (i) => i.patientId !== selectSpecimens.value[0].patientId
            ),
        });
      }

      return action;
    })
  );

  const getButtonByStatus = (status: ESpecimenStatus, isList = false) => {
    let actions: IAction[] = [];

    switch (status) {
      // 未生成
      case ESpecimenStatus.NOTGENERATED:
        actions = [
          {
            label: "打印",
            type: "primary",
            event: "print",
          },
          // {
          //   label: "合管打印",
          //   type: "primary",
          //   event: "merge-print",
          //   visible: isList,
          // },
          {
            label: "作废",
            event: "cancel",
            type: "primary",
          },
        ];
        break;
      // 已生成
      case ESpecimenStatus.Done:
        actions = [
          {
            label: "打印",
            event: "print",
            type: "primary",
          },
          {
            label: "作废",
            event: "cancel",
            type: "primary",
          },
        ];
        break;
      // 已打印条码
      case ESpecimenStatus.PrintedBarCode:
        actions = [
          {
            label: "完成采集",
            type: "primary",
            event: "completeCollection",
          },
          {
            label: "重新打印",
            event: "print",
            type: "primary",
          },
          {
            label: "作废",
            event: "cancel",
            type: "primary",
          },
        ];
        break;
      // 已采集 已送检
      case ESpecimenStatus.Collection:
      case ESpecimenStatus.Submission:
        actions = [
          {
            label: "作废",
            event: "cancel",
            type: "primary",
          },
        ];
        break;
      // 已拒收:
      case ESpecimenStatus.Rejection:
        actions = [
          {
            label: "重新采集",
            type: "primary",
            event: "recollection",
          },
        ];
        break;
      case ESpecimenStatus.Cancelled:
        actions = [
          {
            label: "重新生成",
            type: "primary",
            event: "print",
          },
        ];
        break;
    }

    return actions;
  };

  return {
    listActions,
    getButtonByStatus,
  };
};
